package com.lemon.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.Parameter;

import com.lemon.domain.Menu;
import com.lemon.service.MenuService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class MenuAction extends ActionSupport implements ModelDriven<Menu> {

	private static final long serialVersionUID = 1L;
	private MenuService menuService = new MenuService();

	private Menu menu = new Menu();

	//json数据集合
	private Map<String, Object> json;

	public String resultJSON() {
		ActionContext context = ActionContext.getContext();
		// 获取菜单ID
		String param = getParam(context, "id");
		Long id = Long.valueOf(param);
		// 根据ID查询菜单
		Menu menu = menuService.get(id);
		json = new HashMap<>();
		json.put("id", menu.getId());
		json.put("name", menu.getName());
		json.put("url", menu.getUrl());
		if (menu.getSuperiorMenu() != null) {
			json.put("superiorMenuName", menu.getSuperiorMenu().getName());
		}

		return SUCCESS;
	}

	/**
	 * 添加一个菜单
	 * 
	 * @return
	 */
	public String save() {
		menuService.save(menu);
		return SUCCESS;
	}

	/**
	 * 修改菜单信息
	 * 
	 * @return
	 */
	public String edit() {
		menuService.update(menu);
		return SUCCESS;
	}

	/**
	 * 删除菜单
	 * 
	 * @return
	 */
	public String delete() {
		ActionContext context = ActionContext.getContext();
		// 被删除菜单的ID
		String param = getParam(context, "id");
		Long id = Long.valueOf(param);
		menuService.delete(id);

		return SUCCESS;
	}

	/**
	 * 跳转到修改页面
	 * 
	 * @return
	 */
	public String toEdit() {
		ActionContext context = ActionContext.getContext();
		// 获取要修改的菜单的ID
		String param = getParam(context, "id");
		Long id = Long.valueOf(param);
		// 根据ID查询菜单
		Menu menu = menuService.get(id);
		context.put("menu", menu);

		return SUCCESS;
	}

	/**
	 * 跳转到列表页面
	 * 
	 * @return
	 */
	public String toList() {
		ActionContext context = ActionContext.getContext();
		//生成菜单列表的html字符
		String menuListHtml = menuService.listToHtml();
		context.put("menuListHtml", menuListHtml);

		return SUCCESS;
	}

	/**
	 * 跳转到新增页面
	 * 
	 * @return
	 */
	public String toSave() {
		ActionContext context = ActionContext.getContext();
		List<Menu> menus = menuService.list();
		context.put("menus", menus);
		
		return SUCCESS;
	}

	/**
	 * 封装了参数获取
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	private String getParam(ActionContext context, String key) {
		HttpParameters parameters = context.getParameters();
		Parameter param = parameters.get(key);
		return param.getValue();
	}

	@Override
	public Menu getModel() {
		return menu;
	}

	public Map<String, Object> getJson() {
		return json;
	}

	public void setJson(Map<String, Object> json) {
		this.json = json;
	}

}
